package com.asiainfo.test.service;

import com.asiainfo.test.domain.model.City;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CityService {

    @Resource(name = "db1")
    private SqlSessionTemplate DB1Dao;

    @Resource(name = "db2")
    private SqlSessionTemplate DB2Dao;

    @Resource(name = "db3")
    private SqlSessionTemplate DB3Dao;


    public List<City> listCities() {
        List<City> l1 = this.DB1Dao.selectList("listCities");
        List<City> l2 = this.DB2Dao.selectList("listCities");
        List<City> l3 = this.DB3Dao.selectList("listCities");
        l1.addAll(l2);
        l1.addAll(l3);
        return l1;
    }


    public void saveCities(City city) {
        city.setCountry("db1");
        this.DB1Dao.insert("insert", city);
        city.setCountry("db2");
        this.DB2Dao.insert("insert", city);
        city.setCountry("db3");
        this.DB3Dao.insert("insert", city);
    }

    @Transactional
    public void clearCities() {
        this.DB1Dao.delete("delete");
        this.DB2Dao.delete("delete");
        this.DB3Dao.delete("delete");
    }

}
